package tt.dz.biz.dao.financial;

import java.util.List;
import java.util.Map;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.stereotype.Repository;
import tt.dz.biz.dao.BaseDAO;
import tt.dz.vo.AccountTransferVO;
import tt.dz.vo.TranInfoVO;

@Repository("accountTransferDAO")
public class AccountTransferDAO
  extends BaseDAO
{
  private static final String INSERT_ACCOUNT_TRANSFER = "insert into ms_account_transfer(transfer_seq,merchant_code,source_subject_code,source_subject_balance,target_subject_code,target_subject_balance,amount,state,remark,accounting_date,created_time,created_by,audit_time,audit_by,biz_type)   values(:transferSeq,:merchantCode,:sourceSubjectCode,:sourceSubjectBalance,:targetSubjectCode,:targetSubjectBalance,:amount,:state,:remark,:accountingDate,:createdTime,:createdBy,:auditTime,:auditBy, :bizType)";
  private static final String QUERY_TRAN_INFO = "select receiver_balance as  receiverBalance,  trade_no as   tradeNo, service_no as serviceNo,abstract_code as  abstractCode, thd_end_time as  thdEndTime, payment_amount as  paymentAmount, receiver_name as  receiverName,receiver_type as receiverType, accounting_date as  accountingDate, plat_time as  platTime, check_account_date as  checkAccountDate, pay_state_code as  payStateCode, payer_type as  payerType, thd_pay_seq as  thdPaySeq,batch_no as batchNo, channel_code as  channelCode, is_divided as  isDivided, receiver as  receiver, response_code as  responseCode, payer as  payer, original_payer as  originalPayer, \toriginal_payer_type as originalPayerType,app_id as  appId,check_account_sate as checkAccountSate, reason as  reason, original_payer_name as  originalPayerName,thd_pay_amount as thdPayAmount, tran_time as  tranTime, cancle_status as  cancleStatus,payer_balance as payerBalance, adjust_trade_no as  adjustTradeNo, account_state as  accountState, has_adjust as  hasAdjust, pay_type_code as  payTypeCode, payer_name as  payerName, thd_poundage as  thdPoundage, order_no as  orderNo, card_type as cardType,card_no as cardNo  from  ms_tran_info  mti, ms_user_base ub  where   mti.original_payer = ub.user_id  ";
  private static final String UPDATE_TRAN_INFO_REFUND = "update ms_tran_info  set has_adjust=:hasAdjust ,adjust_trade_no=:adjustTradeNo  where  trade_no=:tradeNo";
  
  public int inertAccountTransfer(AccountTransferVO param)
  {
    return save("insert into ms_account_transfer(transfer_seq,merchant_code,source_subject_code,source_subject_balance,target_subject_code,target_subject_balance,amount,state,remark,accounting_date,created_time,created_by,audit_time,audit_by,biz_type)   values(:transferSeq,:merchantCode,:sourceSubjectCode,:sourceSubjectBalance,:targetSubjectCode,:targetSubjectBalance,:amount,:state,:remark,:accountingDate,:createdTime,:createdBy,:auditTime,:auditBy, :bizType)", param);
  }
  
  public List<TranInfoVO> queryTranInfo(Map map)
  {
    StringBuilder sql = new StringBuilder("select receiver_balance as  receiverBalance,  trade_no as   tradeNo, service_no as serviceNo,abstract_code as  abstractCode, thd_end_time as  thdEndTime, payment_amount as  paymentAmount, receiver_name as  receiverName,receiver_type as receiverType, accounting_date as  accountingDate, plat_time as  platTime, check_account_date as  checkAccountDate, pay_state_code as  payStateCode, payer_type as  payerType, thd_pay_seq as  thdPaySeq,batch_no as batchNo, channel_code as  channelCode, is_divided as  isDivided, receiver as  receiver, response_code as  responseCode, payer as  payer, original_payer as  originalPayer, \toriginal_payer_type as originalPayerType,app_id as  appId,check_account_sate as checkAccountSate, reason as  reason, original_payer_name as  originalPayerName,thd_pay_amount as thdPayAmount, tran_time as  tranTime, cancle_status as  cancleStatus,payer_balance as payerBalance, adjust_trade_no as  adjustTradeNo, account_state as  accountState, has_adjust as  hasAdjust, pay_type_code as  payTypeCode, payer_name as  payerName, thd_poundage as  thdPoundage, order_no as  orderNo, card_type as cardType,card_no as cardNo  from  ms_tran_info  mti, ms_user_base ub  where   mti.original_payer = ub.user_id  ");
    if (null != map)
    {
      if (null != map.get("startDate")) {
        sql.append(" and mti.plat_time>=:startDate ");
      }
      if (null != map.get("endDate")) {
        sql.append(" and mti.plat_time<=:endDate ");
      }
      if (null != map.get("mobileNo")) {
        sql.append(" and ub.mobile_no=:mobileNo ");
      }
    }
    sql.append(" order by mti.plat_time desc");
    MapSqlParameterSource param = new MapSqlParameterSource(map);
    
    return queryForList(sql.toString(), param, TranInfoVO.class);
  }
  
  public int updateTranInfoForRefund(TranInfoVO param)
  {
    return save("update ms_tran_info  set has_adjust=:hasAdjust ,adjust_trade_no=:adjustTradeNo  where  trade_no=:tradeNo", param);
  }
}
